package cn.Y_Qiu.order.service.impl;

import cn.Y_Qiu.basic.exception.BusinessException;
import cn.Y_Qiu.basic.service.impl.BaseServiceImpl;
import cn.Y_Qiu.basic.util.CodeGenerateUtils;
import cn.Y_Qiu.basic.util.PageList;
import cn.Y_Qiu.order.constants.OrderConstants;
import cn.Y_Qiu.order.domain.AdoptOrder;
import cn.Y_Qiu.order.domain.OrderAddress;
import cn.Y_Qiu.order.mapper.AdoptOrderMapper;
import cn.Y_Qiu.order.mapper.OrderAddressMapper;
import cn.Y_Qiu.order.query.AdoptOrderQuery;
import cn.Y_Qiu.order.service.IAdoptOrderService;
import cn.Y_Qiu.org.domain.Employee;
import cn.Y_Qiu.org.mapper.EmployeeMapper;
import cn.Y_Qiu.pay.constants.PayConstants;
import cn.Y_Qiu.pay.domain.PayBill;
import cn.Y_Qiu.pay.mapper.PayBillMapper;
import cn.Y_Qiu.pay.service.impl.PayBillServiceImpl;
import cn.Y_Qiu.pet.domain.Pet;
import cn.Y_Qiu.pet.mapper.PetMapper;
import cn.Y_Qiu.user.domain.LoginInfo;
import cn.Y_Qiu.user.domain.User;
import cn.Y_Qiu.user.domain.UserAddress;
import cn.Y_Qiu.user.mapper.UserAddressMapper;
import cn.Y_Qiu.user.mapper.UserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;

@Service
public class AdoptOrderServiceImpl extends BaseServiceImpl<AdoptOrder> implements IAdoptOrderService {
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private AdoptOrderMapper adoptOrderMapper;
    @Autowired
    private UserAddressMapper userAddressMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private PayBillMapper payBillMapper;
    @Autowired
    private PayBillServiceImpl payBillService;
    @Autowired
    private OrderAddressMapper orderAddressMapper;

    @Override
    @Transactional
    public String submit(Map<String, Object> params, LoginInfo loginInfo) {
        //1.校验参数
        /*service_method:'0',
        address_id: '1',//一定要是t_user_address表中的数据
        pay_method:'1',//  1支付宝  2 微信  0 银联
        pet_id:null,*/
        Integer serviceMethod = Integer.valueOf(params.get("service_method").toString());
        // 数据库中useraddress的id
        Long addressId = Long.valueOf(params.get("address_id").toString());
        Integer payMethod = Integer.valueOf(params.get("pay_method").toString());
        Long petId = Long.valueOf(params.get("pet_id").toString());
        if (serviceMethod == null ||
            payMethod == null ||
            petId == null ||
            addressId == null
                ) {
            throw new BusinessException("参数不完整");
        }
        //2.修改宠物状态 + 下架时间 + 绑定领养人
        User user = userMapper.queryByLoginInfoId(loginInfo.getId());
        Pet pet = petMapper.queryById(petId);
        // 宠物状态state=0 为下架
        pet.setState(0);
        // 绑定领养人的userid
        pet.setUser_id(user.getId());
        // 绑定领养人对象
        pet.setUser(user);
        // 绑定下架日期
        pet.setOffsaletime(new Date());
        petMapper.update(pet);
        //3.创建领养订单
        //3.2 订单本表
        AdoptOrder adoptOrder = iniAdoptOrder(user, pet);
        adoptOrder.setPaySn(CodeGenerateUtils.generateUnionPaySn());
        adoptOrderMapper.add(adoptOrder);
        //3.1 订单地址表
        UserAddress userAddress = userAddressMapper.queryById(addressId);
        OrderAddress orderAddress = iniOrderAddress(adoptOrder, userAddress);
        // 4.支付订单并支付
        PayBill bill = iniPayBill(payMethod, user, adoptOrder);
        orderAddressMapper.add(orderAddress);
        payBillMapper.add(bill);
        String payData = payBillService.request(bill);
        //@TODO 5.超时未支付自动取消
       // System.out.println("没有支付但是金额为" + adoptOrder.getPrice());
        return payData;
    }

    private PayBill iniPayBill(Integer payMethod, User user, AdoptOrder adoptOrder) {
        PayBill bill = new PayBill();
        bill.setDigest(adoptOrder.getDigest());
        bill.setMoney(adoptOrder.getPrice());
        bill.setUnionPaySn(adoptOrder.getOrderSn());
        bill.setState(1);
        bill.setLastPayTime(new Date(System.currentTimeMillis() + OrderConstants.LAST_PAY_TIME));
        bill.setPayChannel(Long.valueOf(payMethod));
        bill.setBusinessKey(adoptOrder.getId());
        bill.setBusinessType(PayConstants.BUSINESS_TYPE_ADOPT);
        bill.setCreateTime(new Date());
        bill.setUser_id(user.getId());
        bill.setShop_id(adoptOrder.getShop_id());
        bill.setNickName(user.getUsername());
        /*
        private String businessType;???
        private Long businessKey;
        private Date createTime;
        private Long user_id;
        private Long shop_id;
        private String nickName;
         */
        return bill;
    }

    @Override
    public PageList<AdoptOrder> admin(AdoptOrderQuery adoptOrderQuery, LoginInfo loginInfo) {
        Employee employee = employeeMapper.queryByLoginInfoId(loginInfo.getId());
        if (employee.getShop_id() != null){
            // 说明是平台员工
            adoptOrderQuery.setShopId(employee.getShop_id());
        }
        return super.queryPage(adoptOrderQuery);
    }

    @Override
    public PageList<AdoptOrder> user(AdoptOrderQuery adoptOrderQuery, LoginInfo loginInfo) {
        User user = userMapper.queryByLoginInfoId(loginInfo.getId());
        adoptOrderQuery.setUserId(user.getId());
        return super.queryPage(adoptOrderQuery);
    }

    private OrderAddress iniOrderAddress(AdoptOrder adoptOrder, UserAddress userAddress) {
        OrderAddress orderAddress = new OrderAddress();
        BeanUtils.copyProperties(userAddress, orderAddress);
        orderAddress.setOrder_id(adoptOrder.getId());
        orderAddress.setOrderSn(adoptOrder.getOrderSn());
        orderAddress.setUpdateTime(new Date());
        return orderAddress;
    }

    private AdoptOrder iniAdoptOrder(User user, Pet pet) {
        AdoptOrder adoptOrder = new AdoptOrder();
        /*
        * 需要设置字段
            digest
            price
            orderSn
            paySn
            pet_id
            user_id
            shop_id
            address_id
        *
        *
        */
        adoptOrder.setDigest("[摘要]对" + pet.getName() + "领养订单！");
        adoptOrder.setPrice(pet.getSaleprice());
        adoptOrder.setOrderSn(CodeGenerateUtils.generateOrderSn(user.getId()));
        //adoptOrder.getPaySn() 还没写到支付
        adoptOrder.setPet_id(pet.getId());
        adoptOrder.setUser_id(user.getId());
        adoptOrder.setShop_id(pet.getShop_id());
        adoptOrder.setLastPayTime(new Date(System.currentTimeMillis() + OrderConstants.LAST_PAY_TIME));
        return adoptOrder;
    }
}
